home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_484 / msclock / source / msclock-handler.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  228 lines

  1. /*
  2.  *    MSClock-Handler.c        © 12.02.91 by Martin Steppler
  3.  */
  4.  
  5. // includes
  6.  
  7. #include <stdio.h>
  8. #include <libraries/dos.h>
  9. #include <libraries/dosextens.h>
  10. #include <devices/inputevent.h>
  11. #include <devices/printer.h>
  12. #include <devices/timer.h>
  13. #include <devices/input.h>
  14. #include <graphics/gfx.h>
  15. #include <graphics/gfxbase.h>
  16. #include <graphics/gfxmacros.h>
  17. #include <intuition/intuition.h>
  18. #include <intuition/intuitionbase.h>
  19. #include <exec/exec.h>
  20. #include <hardware/cia.h>
  21. #include <functions.h>
  22.  
  23. // prototypes
  24.  
  25. void ciao(void);
  26.  
  27. // reference to the absolute address of ciab
  28.  
  29. extern struct CIA ciab;
  30.  
  31. // defines
  32.  
  33. #define SIG_CHANGE   SIGBREAKF_CTRL_C
  34. #define SIG_CLOSE    SIGBREAKF_CTRL_D
  35. #define CHILD_READY  SIGBREAKF_CTRL_D
  36. #define EOS 0
  37. #define PORTNAME "Martin's Clock-Port"
  38.  
  39. struct TheClock
  40. {
  41.    struct MsgPort    Port;          // Global messageport.
  42.    BPTR              Segment;       // Pointer to handler segment.
  43.    LONG              SegSize;       // Size of TheClock structure.
  44.    struct Task      *Father;        // Calling process.
  45.    struct Task      *Child;         // Waiting process
  46.    int               LeftEdge;      // LeftEdge of our window
  47.    int               Width;         // Width of our window
  48.    int               Online;        // Online flag
  49.    int               Flags;         // display-flags
  50.    int               FlagsBackup;   // backup of the display-flags
  51.    int               Status;        // status-flag
  52. };
  53.  
  54. // globals
  55.  
  56. char                  DateBuf[60];
  57. struct TextAttr       TopazText    = { (UBYTE *)"topaz.font",8,0,0};
  58. struct IntuiText      TextPrint    = { 0,1,JAM2,0,0,NULL,(UBYTE *)DateBuf,NULL};
  59. struct NewWindow      ClockWindow  = { 146,1,432,8,1,0,EOS, BORDERLESS, NULL,NULL,EOS,NULL,NULL,0,0,0,0,WBENCHSCREEN};
  60. struct Window        *Window=NULL;
  61. struct timerequest    TimeReq;
  62. struct MsgPort       *TimerPort=NULL;
  63. struct IntuitionBase *IntuitionBase;
  64. struct GfxBase       *GfxBase;
  65. struct DosLibrary    *DosBase;
  66. struct TheClock      *TheClock;
  67.  
  68.         /* _main():
  69.          *
  70.          *      Modified Aztec C startup-routine, no CLI parsing,
  71.          *      no Workbench parsing, absolutely nothing.
  72.          */
  73.  
  74. long
  75. _main()
  76. {
  77.    register struct Process *ThatsMe = (struct Process *)FindTask(NULL);
  78.    register ULONG SignalSet;
  79.    char TempBuf[60];
  80.    char *Days[14]={ "Son","Mon","Die","Mit","Don","Fre","Sam",
  81.                     "Sun","Mon","Tue","Wed","Thu","Fri","Sat" };
  82.    struct DateStamp Date;
  83.    long n,eng,Month,Day,Year,chip_free,fast_free,hh=0,mm=0,ss=0,lastsec=-1;
  84.    int i;
  85.  
  86.       // If somebody called us from CLI...
  87.  
  88.    if(ThatsMe -> pr_CLI) return(10);
  89.  
  90.       // Is the TheClock structure anywhere?
  91.  
  92.    if(!(TheClock = (struct TheClock *)FindPort(PORTNAME))) return(10);
  93.  
  94.       // open libraries
  95.  
  96.    if(!(IntuitionBase = OpenLibrary("intuition.library",0))) {
  97.       Signal(TheClock -> Father, CHILD_READY);
  98.       return(20);
  99.    }
  100.    if(!(GfxBase       = OpenLibrary("graphics.library",0))) {
  101.       CloseLibrary(IntuitionBase);
  102.       Signal(TheClock -> Father, CHILD_READY);
  103.       return(20);
  104.    }
  105.    if(!(DosBase       = OpenLibrary("dos.library",0))) {
  106.       CloseLibrary(GfxBase);
  107.       CloseLibrary(IntuitionBase);
  108.       Signal(TheClock -> Father, CHILD_READY);
  109.       return(20);
  110.    }
  111.  
  112.    if(TimerPort=(struct MsgPort *) CreatePort("Timer Port",0)) {
  113.       if(OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *)&TimeReq,0)==(long)NULL) {
  114.          TimeReq.tr_node.io_Message.mn_ReplyPort=TimerPort;
  115.          TimeReq.tr_node.io_Command=TR_ADDREQUEST;
  116.          TimeReq.tr_node.io_Flags=0;
  117.          TimeReq.tr_node.io_Error=0;
  118.          TimeReq.tr_time.tv_secs=0;
  119.          TimeReq.tr_time.tv_micro=1000000;  // delay
  120.          SendIO((struct IORequest *) &TimeReq.tr_node);
  121.       }
  122.       else ciao();
  123.    }
  124.    else ciao();
  125.  
  126.    TheClock -> Child = (struct Task *)FindTask(NULL);
  127.    TheClock -> Status = 1;
  128.  
  129.            // Go into infinite loop waiting for signals.
  130.  
  131.    FOREVER
  132.    {
  133.       if(TheClock -> Status == 1) {      // switch clock on
  134.          TheClock -> Status = 2;
  135.          ClockWindow.LeftEdge = TheClock -> LeftEdge;
  136.          ClockWindow.Width    = TheClock -> Width;
  137.          if(Window=(struct Window *) OpenWindow(&ClockWindow)) {
  138.             if(Window->IFont->tf_YSize>8) TextPrint.ITextFont=&TopazText;
  139.             TheClock->Status = 0;
  140.          }
  141.          else ciao();
  142.  
  143.            // Tell father to finish.
  144.  
  145.          Signal(TheClock -> Father, CHILD_READY);
  146.       }
  147.       if(!TheClock->Status) {     // show mem, date, online-time and/or time
  148.          chip_free = AvailMem(MEMF_CHIP) >> 10;
  149.          fast_free = AvailMem(MEMF_FAST) >> 10;
  150.          DateStamp((struct DateStamp *)&Date) ;
  151.          n = Date.ds_Days - 2251;
  152.          Year = (4 * n + 3) / 1461;
  153.          n -= 1461 * Year / 4;
  154.          Year += 1984;
  155.          Month = (5 * n + 2) / 153;
  156.          Day = n - (153 * Month + 2) / 5 + 1;
  157.          Month += 3; n=TheClock->Flags;
  158.          if(Month>12) { Year++; Month -= 12; }
  159.          DateBuf[0]=EOS;
  160.          if(n & 4) sprintf(DateBuf, "A:%-4d F:%-4d C:%-3d", chip_free+fast_free, fast_free, chip_free);
  161.          if(n & 32) {
  162.  
  163.             // Check port a of cia b. Is there a carrier?
  164.  
  165.             if(!(ciab . ciapra & CIAF_COMCD)) // Online ?
  166.             {
  167.                if(!TheClock -> Online) {    // Time reset
  168.                   hh=mm=ss=0; TheClock -> Online = TRUE;
  169.                }
  170.  
  171.                        // Increment time counter.
  172.  
  173.                if(lastsec != Date.ds_Tick/TICKS_PER_SECOND) {
  174.                   lastsec = Date.ds_Tick/TICKS_PER_SECOND;
  175.                   if(++ss == 60) {
  176.                      ss=0; if(++mm == 60) { mm=0; ++hh; }
  177.                   }
  178.                }
  179.             }
  180.             else TheClock -> Online = FALSE;
  181.             sprintf(TempBuf, "  %02ld:%02ld:%02ld", hh, mm, ss);
  182.             strcat(DateBuf,TempBuf);
  183.          }
  184.          if(n & 2) {
  185.             eng = (n & 8) ? 7:0;
  186.             if(n & 16) sprintf(TempBuf, "  %s %02ld-%02ld-%02ld", Days[(Day + Year + (Year - (Month < 3)) / 4 + 3 * Month - 2 * (Month > 2) - (Month - 1 - (Month > 8)) / 2 + 2) % 7 +eng], Month, Day, Year-1900);
  187.             else       sprintf(TempBuf, "  %s %02ld.%02ld.%02ld", Days[(Day + Year + (Year - (Month < 3)) / 4 + 3 * Month - 2 * (Month > 2) - (Month - 1 - (Month > 8)) / 2 + 2) % 7 +eng], Day, Month, Year-1900);
  188.             strcat(DateBuf,TempBuf);
  189.          }
  190.          if(n & 1) {
  191.             sprintf(TempBuf, "  %02ld:%02ld:%02ld", Date.ds_Minute/60, Date.ds_Minute%60, Date.ds_Tick/TICKS_PER_SECOND);
  192.             strcat(DateBuf, TempBuf);
  193.          }
  194.          PrintIText(Window->RPort,&TextPrint, 0,0);
  195.       }
  196.       SignalSet = Wait(SIG_CLOSE | SIG_CHANGE | (1 << TimerPort->mp_SigBit));
  197.       if(SignalSet & SIG_CLOSE) {
  198.          ciao();
  199.          CloseLibrary(IntuitionBase);
  200.          CloseLibrary(GfxBase);
  201.          CloseLibrary(DosBase);
  202.          Forbid();
  203.          Signal(TheClock -> Father, CHILD_READY);
  204.          return(0);
  205.       }
  206.       else if(SignalSet & SIG_CHANGE) {
  207.          if(Window) { CloseWindow(Window); Window = NULL; }
  208.          TheClock->Flags = TheClock->FlagsBackup;
  209.          TheClock->Status = 1;
  210.       }
  211.       if(SignalSet & (1 << TimerPort->mp_SigBit)) {
  212.          if(GetMsg(TimerPort)) {
  213.             TimeReq.tr_time.tv_secs=0;
  214.             TimeReq.tr_time.tv_micro=1000000;
  215.             SendIO((struct IORequest *) &TimeReq.tr_node);
  216.          }
  217.       }
  218.    }
  219. }
  220. void
  221. ciao(void)
  222. {
  223.    if(TimerPort) GetMsg(TimerPort);
  224.    if(TimeReq.tr_node.io_Message.mn_ReplyPort) CloseDevice((struct IORequest *)&TimeReq);
  225.    if(TimerPort) { DeletePort(TimerPort); TimerPort = NULL; }
  226.    if(Window) { CloseWindow(Window); Window = NULL; }
  227. }
  228.